﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using System.IO;
using MSword = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using SMALLERP;

namespace XJLSC.PM
{
    public partial class SCJHQCZZ : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        private SqlDataReader sdr;

        public SCJHQCZZ()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT SUBSTRING(COPTD.CREATE_DATE,1,8) 订单录入日期,TD001 单别,TD002 单号,TD003 序号,TD004 品号,TD005 品名,TD006 规格,RTRIM(TC015)+RTRIM(TD020) AS 备注,CEILING(TD008) 订单数量,CEILING(TD009) 已交数量,CEILING(TD008-TD009) 未交数量,TD013 交货日期,CEILING(ISNULL(MC007,0)) 成品库存,CEILING(BZJH) 包装下单,CEILING(BZWC) 包装完成,ISNULL(BZPC,'') 包装排产,ISNULL(PQPC,'') 喷漆排产,0 AS 杯体库存,0 AS 抛光未回,0 AS 电解未回,CEILING(ZHJH) 综合下单,CEILING(ZHWC) 综合完成,ISNULL(ZHPC,'') 综合排产,CEILING(NDJH) 内胆下单,CEILING(NDWC) 内胆完成,ISNULL(NDPC,'') 内胆排产,CEILING(WKJH) 外壳下单,CEILING(WKWC) 外壳完成,ISNULL(WKPC,'') 外壳排产,'' 电解品号,'' 抛光品号 FROM COPTD LEFT JOIN COPTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN COPMA ON TC004=MA001 LEFT JOIN INVMC ON TD004=MC001 AND MC002='108' LEFT JOIN V_SCZZHZ ON TD001=UZ003 AND TD002=UZ004 AND TD003=UZ005 WHERE TD001='2210' AND TD016='N' AND (TD002 LIKE '%" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (TD005 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='') AND (MA003 LIKE '%" + textBox3.Text + "%' OR '" + textBox3.Text + "'='')";
            try
            {
                DataTable dt;
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                dt.Rows.Add();
                gridControl1.DataSource = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }

            //progressBar1.Visible = true;
            //progressBar1.Maximum = dataGridView1.Rows.Count;//设置最大长度值
            //progressBar1.Value = 0;//设置当前值
            //progressBar1.Step = 1;//设置没次增长多少
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 2; i++)
            //{
            //    progressBar1.Value += progressBar1.Step;
              
                
            //}
            //progressBar1.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string picPath = "";
            string wdbh="";
             string strSql = "SELECT RTRIM(DocID) DocID FROM ATTACH WHERE Parent='COPI06' AND KeyValues='" + dataGridView1.CurrentRow.Cells["单别"].Value.ToString() + "||" + dataGridView1.CurrentRow.Cells["单号"].Value.ToString() + "'";
            
            try
            {
                sdr = db.GetDataReader(strSql);
                sdr.Read();
                if (sdr.HasRows)
                {
                 wdbh=sdr["DocID"].ToString();
               
                }
                else
                {
                    MessageBox.Show("没有找到产品图纸", "系统提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            finally
            {
                sdr.Close();
            }

            
            picPath = "\\\\192.168.0.208\\DocLibrary\\" + wdbh.Substring(3,4) + "000\\" + wdbh + ".001";
        
            if (!File.Exists(picPath))
            {
                MessageBox.Show("没有找到产品图纸", "系统提示");
                return;
            }
            //System.Diagnostics.Process.Start("rundll32.exe", picPath);
                        //新建一个应用程序EXC1
            MSword.Application EXC1 = new MSword.Application();
            EXC1.Visible = true;//设置EXC1打开后可见


            MSword.Workbooks wbs = EXC1.Workbooks;
            MSword._Workbook wb = wbs.Add(picPath);//打开并显示EXCEL文件

            //ProcessStartInfo psi = new ProcessStartInfo(this.FilePath + "\\" + this.FileName);
            //Process pro = new Process();
            //pro.StartInfo = psi;
            //pro.Start();


        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private void 查看订单BOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2.PerformClick();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }

        private void 查看采购信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button4.PerformClick();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0;
            string x4 = "";
            string x5 = "";
            string x6 = "";
            string x7 = "";

            x1 = dataGridView1["单别", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            x2 = dataGridView1["单号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            x3 = 0.00;
            x5 = dataGridView1["序号", dataGridView1.CurrentCell.RowIndex].Value.ToString();

            x4 = "查看订单采购状况";
            MXCX xx = new MXCX(x1, x2, x3, x4, x5);
            CHAXUN f = new CHAXUN();
            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void 查看详细加工进度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button5.PerformClick();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            string x3 = "";
            string x4 = "";
            string x5 = "";
            x1 = dataGridView1["单别", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            x2 = dataGridView1["单号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            x3 = dataGridView1["序号", dataGridView1.CurrentCell.RowIndex].Value.ToString();

            if (dataGridView1.CurrentCell.OwningColumn.HeaderText=="包装完成")
            {
                x5 = "1008";
            }
            else if(dataGridView1.CurrentCell.OwningColumn.HeaderText == "综合完成")
            {
                x5 = "1002";
            }
            else if (dataGridView1.CurrentCell.OwningColumn.HeaderText == "外壳完成" || dataGridView1.CurrentCell.OwningColumn.HeaderText == "内胆完成")
            {
                x5 = "1001";
            }
            else
            {
                x5 = "其他";
            }
         
            x4 = "查看生产进度";
            MXCXB xx = new MXCXB(x1, x2, x3, x4, x5);
            CHAXUN f = new CHAXUN();
            f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }
    }
}
